
 /**
 * BaseException.java.java.
 * User: BrianChen Date: 2012-5-22 下午05:07:42
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-5-22 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-5-22 下午05:07:42
 * @version 1.0
 * @see
 * 
 */
@SuppressWarnings("serial")
public class BaseException extends RuntimeException{
	private String errorKey = null;
	private String message = null;
	private Throwable nested = null;
	
    /**
     * Creates new <code>BaseException</code> without detail message.
     */
	public BaseException(){
		super();
	}
    /**
     * Constructs an <code>BaseException</code> with the specified nested Exception.
     * 
     * @param nested the specified nested Exception.
     */
	public BaseException(Throwable nested){
		super();
		this.nested = nested;
	}
    /**
     * Constructs an <code>BaseException</code> with the specified detail message.
     * 
     * @param msg the detail message.
     */
	public BaseException(String msg){
		super(msg);
		this.message = msg;
	}
    /**
     * Constructs an <code>BaseException</code> with the specified detail message,errorKey.
     * 
     * @param errorKey the message key.
     * @param msg the detail message.
     */
	public BaseException(String errorKey, String msg){
		super(msg);
		this.errorKey = errorKey;
		this.message = msg;
	}
    /**
     * Constructs an <code>BaseException</code> with the specified detail message and nested Exception.
     * 
     * @param msg the detail message.
     * @param nested the specified nested Exception.
     */
	public BaseException(String msg, Throwable nested){
		super(msg);
		this.message = msg;
		this.nested = nested;
	}
    /**
     * Constructs an <code>BaseException</code> with the specified detail message,errorKey and nested Exception.
     * 
     * @param errorKey the message key.
     * @param msg the detail message.
     * @param nested the specified nested Exception.
     */
	public BaseException(String errorKey, String msg, Throwable nested){
		super(msg);
		this.errorKey = errorKey;
		this.message = msg;
		this.nested = nested;
	}
	
    /**
     * Returns the detail message, NOT including the message from the nested exception.
     */
	public String getNonNestedMessage(){
		if(this.message != null){
			return this.message;
		}else{
			return super.getMessage();
		}
	}
	/**
     * Prints the composite message to System.err.
     */
    public void printStackTrace(){
        super.printStackTrace();
        if (this.nested != null){
           this.nested.printStackTrace();
        }
    }
    /**
     * Prints the composite message and the embedded stack trace to the specified stream ps.
     */
    public void printStackTrace(PrintStream ps){
       super.printStackTrace(ps);
       if (this.nested != null){
           this.nested.printStackTrace(ps);
       }
    }
    /**
     * Prints the composite message and the embedded stack trace to the specified print writer pw.
     */
    public void printStackTrace(PrintWriter pw){
    	super.printStackTrace(pw);
        if (this.nested != null){
            this.nested.printStackTrace(pw);
        }
    }
    /**
     * Returns the nested exception if there is one, null if there is not.
     */
	public Throwable getNested() {
		if(this.nested != null){
			return nested;
		}else{
			return this;
		}
	}
	public void setNested(Throwable nested) {
		this.nested = nested;
	}
	public String getErrorKey() {
		return errorKey;
	}
	public void setErrorKey(String errorKey) {
		this.errorKey = errorKey;
	}
	/**
	 * Returns the detail message, including the message from the nested exception if there is one.
	 */
	public String getMessage() {
		if(this.nested != null){
			return this.message + " (" + nested.getMessage() + ")";
		}else{
			return this.getNonNestedMessage();
		}
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
}

